<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <meta http-equiv="x-ua-compatible" content="ie=edge" />
    <include file="./Public/Admin/header.html" />
</head>
<body>

<div id="main">
    <div class="container-fluid">

        <div class="row-fluid">
            <div class="span12">
                <div class="box">
                    <div class="box-title">
                        <div class="span10">
                            <h3><i class="icon-edit"></i>编辑用户角色</h3>
                        </div>
                        <div class="span2"><a class="btn" href="javascript:window.history.go(-1)">返回</a></div>
                    </div>

                    <div class="box-content">
                        <!--<form action="admin/main/addtypes" method="post" class="form-horizontal form-validate">-->
                        <div class="control-group">
                            <label for="user_type_name" class="control-label">角色名称：</label>
                            <div class="controls">
                                <input type="text" name="user_type_name" id="user_type_name" class="input-medium" value="<?php echo $type['user_type_name']?>"/><span class="maroon">*</span>
                            </div>
                        </div>
                        <div class="control-group">
                            <label for="sort_num" class="control-label">管理模块：</label>
                            <div class="controls">
                                <table id="listtable" class="table table-bordered table-hover  datatable">
                                    <thead>
                                    <tr>
                                        <th class='with-checkbox'>
                                            <input type="checkbox" class="check_all" /></th>
                                        <th>菜单名称</th>
                                    </tr>
                                    </thead>
                                    <tbody>

                                    <?php foreach($menus as $item): $a=false;?>
                                    <tr>
                                        <td class="with-checkbox">
                                            <?php if(count($module)>0):foreach($module as $m):?>
                                            <?php if($m->sys_menu_id==$item['sys_menu_id']): $a=true; ?>
                                            <input type="checkbox" checked="checked" attr="check-menu_<?php echo $item['sys_menu_id']?>" name="check" value="<?php echo $item['sys_menu_id'];?>">
                                            <?php endif;?>
                                            <?php endforeach;endif;?>

                                            <?php if(!$a):?>
                                            <input type="checkbox" name="check" attr="check-menu_<?php echo $item['sys_menu_id']?>" value="<?php echo $item['sys_menu_id'];?>">
                                            <?php endif;?>
                                        </td>
                                        <td style="text-align:left;">
                                            <?php echo $item['sys_menu_name'];?>
                                        </td>
                                    </tr>
                                    <?php
                                    $childmenus=M('sys_menu')->where(array('parent_id'=>$item['sys_menu_id']))->order('sort_num asc')->select();
                                    if(count($childmenus)>0):foreach($childmenus as $child):$b=false;
                                    ?>
                                    <tr>
                                        <td class="with-checkbox">
                                            <?php if(count($module)>0):foreach($module as $m):?>
                                            <?php if($m->sys_menu_id==$child['sys_menu_id']): $b=true; ?>
                                            <input type="checkbox" checked="checked" attr="z-check-menu_<?php echo $child['parent_id']?>_<?php echo $child['sys_menu_id']?>" name="check" value="<?php echo $child['sys_menu_id'];?>">
                                            <?php endif;?>
                                            <?php endforeach;endif;?>

                                            <?php if(!$b):?>
                                            <input type="checkbox" name="check" attr="z-check-menu_<?php echo $child['parent_id']?>_<?php echo $child['sys_menu_id']?>" value="<?php echo $child['sys_menu_id'];?>">
                                            <?php endif;?>

                                        </td>
                                        <td style="text-align:left;padding-left:24px;">
                                            <img src="__PUBLIC__img/t.gif"/>
                                            <?php echo $child['sys_menu_name'];?>
                                        </td>
                                    </tr>
                                    <?php
                                    $sjmenus=M('sys_menu')->where(array('parent_id'=>$child['sys_menu_id']))->order('sort_num asc')->select();
                                    if(count($sjmenus)>0):foreach($sjmenus as $sj):$b=false;
                                    ?>
                                    <tr>
                                        <td class="with-checkbox">
                                            <?php if(count($module)>0):foreach($module as $m):?>
                                            <?php if($m->sys_menu_id==$sj['sys_menu_id']): $b=true; ?>
                                            <input type="checkbox" checked="checked" name="check" attr="s-check-menu_<?php echo $sj['parent_id']?>_<?php echo $sj['sys_menu_id']?>" value="<?php echo $sj['sys_menu_id'];?>">
                                            <?php endif;?>
                                            <?php endforeach;endif;?>

                                            <?php if(!$b):?>
                                            <input type="checkbox" name="check" attr="s-check-menu_<?php echo $sj['parent_id']?>_<?php echo $sj['sys_menu_id']?>" value="<?php echo $sj['sys_menu_id'];?>">
                                            <?php endif;?>

                                        </td>
                                        <td style="text-align:left;padding-left:48px;">
                                            <img src="__PUBLIC__img/t.gif"/>
                                            <?php echo $sj['sys_menu_name'];?>
                                        </td>
                                    </tr>
                                    <?php endforeach;endif;?>
                                    <?php endforeach;endif;?>

                                    <?php endforeach;?>

                                    </tbody>
                                </table>
                            </div>
                        </div>

                        <div class="form-actions">
                            <input type="hidden" name="action" id="action" value="<?php echo $action?>"/>
                            <input type="hidden" name="user_type_id" id="user_type_id" value="<?php echo $user_type_id?>"/>
                            <button type="submit" id="btnsave" class="btn btn-primary">保存</button>
                            <a class="btn" href="javascript:window.history.go(-1)">取消</a>

                        </div>
                        <!--</form>-->
                    </div>
                </div>
            </div>

        </div>
    </div>
</div>
<script type="text/javascript">
    $(function(){
        $("#btnsave").click(function(){
            var user_type_name=$("#user_type_name").val();
            if(user_type_name==''||user_type_name==null)
            {
                alert('请填写角色名称');
                return false;
            }
            var action=$("#action").val();
            var user_type_id=$("#user_type_id").val();
            var check = $("input[name='check']:checked");
            /*if(check.length<1){
             alert('请选择删除项');
             return false;
             }*/
            var id = [];
            check.each(function(i){
                id[i] = $(this).val();
            });

            $.post("{:U('Admin/Permission/addtypes')}",{'user_type_name':user_type_name,'user_type_id':user_type_id,'action':action,'menusid':id},function(data){
                window.location.href=data.url;
                //alert("");
            },'json');
        });

        $(".check_all").live('change',function(){
            if($(this).attr("checked")){
                $("input[name='check']").attr("checked",true);
            }else{
                $("input[name='check']").attr("checked",false);
            }
        });

        $("input[attr^='check-menu_']").live('change',function(){
            var tmp = $(this).attr('attr').split('_');
            var mid=tmp[1];
            if($(this).attr("checked")){
                $("input[attr^='z-check-menu_"+mid+"_']").attr("checked",true);
            }else{
                $("input[attr^='z-check-menu_"+mid+"_']").attr("checked",false);
            }

        });

        $("input[attr^='z-check-menu_']").live('click',function(){
            var tmp=$(this).attr('attr').split('_');
            var mid=tmp[1];
            var len=$("input[attr^='z-check-menu_"+mid+"_']").length;
            var b=$("input[attr^='z-check-menu_"+mid+"_']").filter(":checked").length;
            if($(this).attr("checked"))
            {
                $("input[attr^='s-check-menu_"+mid+"_']").attr("checked",true);
            }
            else{
                $("input[attr^='s-check-menu_"+mid+"_']").attr("checked",false);
            }
            if(b>0){
                var flag=$("input[attr='check-menu_"+mid+"']").prop("checked",true);
            }
            else
            {
                var flag=$("input[attr='check-menu_"+mid+"']").prop("checked",false);

            }
        });
    });
</script>
</body>
</html>
